<include file="public:header" />
<include file="public:left" />
<style>
    #select-area select{height:30px;}
    .font_add{font-size:25px;width:20px;height: 20px;display: inline-block;}
    .font_cut{font-size:25px;width:20px;height: 20px;display: inline-block;}
    .font_style{font-size:25px;width:20px;height: 20px;display: inline-block;}
</style>
<form  method="post" enctype="multipart/form-data" id="brandForm" name="brandForm">
    <section>
        <h2><strong>添加抽奖电话</strong> <a href="javascript:void(0)" class="font_add">+</a></h2>
        <ul class="ulColumn2">

        </ul>
        <div style="margin-left: 50px">
            <span class="item_name" style="width:120px;"></span>
            <input type="button" class="link_btn" value="提交"/>
        </div>
    </section>
    <input type="hidden" name="activity_id" value="{$activity_id}">
</form>
<!--tabStyle-->
<style type="text/css" src="__STATIC__/admin/js/laydate/need/laydate.css" ></style>
<script src="__STATIC__/admin/js/laydate/laydate.js"></script>
</div>
</section>
<script>
    $(function(){
        $('.ulColumn2').append('<li><span class="item_name" style="width:120px;">手机号码：</span><input type="text" class="textbox textbox_295" name="mobile[]" placeholder="电话" value=""/></li>');
    });

    $('.font_add').click(function(){
        $('.ulColumn2').append('<li><span class="item_name" style="width:120px;">手机号码：</span><input type="text" class="textbox textbox_295" name="mobile[]" placeholder="电话" value=""/>&nbsp;&nbsp;<a href="javascript:void(0)" onclick="font_style(this)" class="font_style">-</a></li>');
    });

    function font_style(obj){
        $(obj).parent().remove();
    }

    /*判断输入是否为合法的手机号码*/
    function isphone(mobile)
    {
        var partten = /^1[3,5,8]\d{9}$/;
        var fl=false;
        if(partten.test(mobile))
        {
           return true;
        }else{
           return false;
        }
    }

    $('.link_btn').click(function(){
        var bool = true;
        var mobile = new Array();
        var activity_id = $("input[name='activity_id']").val();
        var i=0;
        $('.ulColumn2 li').find("input[name='mobile[]']").each(function(){
            var val = $(this).val();
            if(val == ''){
                bool = false;
                $(this).focus();
                return;
            }

            if(!isphone(val)){
                bool = false;
                $(this).focus();
                return;
            }

            mobile[i]=val;
            i++;
        });

        if(!bool){
            alert('手机号码不能为空并且输入规范号码!');
            return;
        }

        var mobile=mobile.sort();
        var newmobile = new Array();
        var z=0;

        for(var i=0;i<mobile.length;i++){  
            if(!IsInArray(newmobile,mobile[i])){
                newmobile[z]=mobile[i];
                z++;
            }
        }  

        var formData = new FormData();
        formData.append('mobile',newmobile);
        formData.append('activity_id',activity_id);

        $.ajax({
            url:"{:url('Activity/lotteryer_add')}",
            type:'POST',
            data:formData,
            async:false,
            processData:false,
            contentType:false,
            success:function(result){
                if(result.code == '0'){
                        layer.alert(result.msg);
                }else{
                    layer.alert(result.msg, {
                                btn: ['确定']
                            },
                    function (index, item) {
                        location.href = result.url;
                    });
                }
            }
        });
    });

    function IsInArray(arr,val){ 
    　　var testStr=','+arr.join(",")+","; 
     
    　　return testStr.indexOf(","+val+",")!=-1;
    } 
</script>
</body>
</html>
